
'use strict';

const BaseService = require("./base");


class TagsService extends BaseService{
    // 获取标签列表
    async tagsList(body){
        const params = this.app._.cloneDeep(body);
        if(params.all){
            const rows = await this.app.model.Tags.findAll({
                attributes:{exclude:['delete_time']},
            })
            return rows
        }else{
            // 筛选项：根据名称模糊查询
            const {count,rows} = await this.app.model.Tags.findAndCountAll({
                distinct:true,
                where:this.queryWhere(params),
                attributes:{exclude:['delete_time']}, 
                ...this.pageParam(params)
            });
            return this.paginate(count, rows,params);
        }
    }
    
    // 筛选项
    queryWhere(params){
        const {Op} = this.app.Sequelize
        const {searchWord} = params
        const where = {}
        if(searchWord!==''){
            where[Op.and] = {tag_name:{[Op.like]:`%${searchWord}%`}}
        }
        return where
    }

    // 创建标签
    async createTags(body){
        const params = this.app._.cloneDeep(body)
        params.create_time = new Date()
        await this.app.model.Tags.create(params)
    }
    //删除标签 (批量)
    async deleteTags(ids){
        const { Op } = this.app.Sequelize;
        await this.app.model.Tags.destroy({
          where:{
            id:{
              [Op.in]:ids
            }
          }
        })
    }

    //编辑标签
    async updateTags(body){
        console.log(body);
        const {id,tag_name,tag_describe,tag_img} = body
        await this.app.model.Tags.update({ tag_name,tag_describe, tag_img,update_time:new Date()},{
            where:{
                id:id
            }
        })
    }
}









module.exports = TagsService;